#include "SuperConfig.h"
#include "SuperApi.h"

CSuperConfig::CSuperConfig(void)
{
}

CSuperConfig::~CSuperConfig(void)
{
}

void CSuperConfig::GetApiList( QList<CSuperApi*>& listApi )
{
        QMap<ApiType,CSuperApi*>::const_iterator iter=m_mapApi.begin();
        for(;iter!=m_mapApi.end();++iter){
                listApi.push_back(iter.value());
        }
}

CSuperApi* CSuperConfig::GetApiByType( ApiType type )
{
	QMap<ApiType,CSuperApi*>::const_iterator iter=m_mapApi.find(type);
	if(iter!=m_mapApi.end()){
		return iter.value();
	}
	else
		return 0;
}

bool CSuperConfig::InsertApi( CSuperApi* pApi )
{
	if(m_mapApi.find(pApi->GetApiData()->type)!=m_mapApi.end())
		return false;
	m_mapApi[pApi->GetApiData()->type]=pApi;
	return true;
}

bool CSuperConfig::RemoveApi( CSuperApi* pApi )
{
	m_mapApi.remove(pApi->GetApiData()->type);
	return true;
}

bool CSuperConfig::ReadConfig()
{
	QString inputFilePath(/*qApp->applicationDirPath()+"/config.xml"*/SUPER_CONFIG_NAME);
	QFile inputFile(inputFilePath);

	if (!QFile::exists(inputFilePath))
		return false;
	else if (!inputFile.open(QIODevice::ReadOnly))
		return false;
	QXmlStreamReader reader(&inputFile);

//	QXmlStreamReader::atEnd();
	while(!reader.atEnd()){
		if(reader.name()=="API"){
			ReadApi(reader);
		}
		reader.readNextStartElement();
	}
	return true;
}

bool CSuperConfig::WriteConfig()
{
	QString outputFilePath(/*qApp->applicationDirPath()+"/config.xml"*/SUPER_CONFIG_NAME);
	QFile outputFile(outputFilePath);

	if (QFile::exists(outputFilePath))
		QFile::remove(outputFilePath);
	if (!outputFile.open(QIODevice::WriteOnly))
		return false;
	QXmlStreamWriter writer(&outputFile);
	writer.writeStartDocument();
	writer.writeStartElement("Root");
        QMap<ApiType,CSuperApi*>::const_iterator iter=m_mapApi.begin();
        for(;iter!=m_mapApi.end();++iter){
		stSuperApi* pSuperApi=iter.value()->GetApiData();
		WriteApi(writer,pSuperApi);
	}
	writer.writeEndElement();
	writer.writeEndDocument();
	return true;
}

void CSuperConfig::ReadApi( QXmlStreamReader& reader )
{
	Q_ASSERT(reader.isStartElement() && reader.name() == "API");
	stSuperApi* pSuperApi=new stSuperApi;
	while(reader.readNextStartElement()){
		if (reader.name() == "Type")
			ReadType(reader,pSuperApi);
		else if (reader.name() == "AuthAccess")
			ReadAccess(reader,pSuperApi);
		else if (reader.name() == "AuthAccessScret")
			ReadAccessScret(reader,pSuperApi);
		else if (reader.name() == "UserId")
			ReadUserId(reader,pSuperApi);
		else if (reader.name() == "Icon")
			ReadIconFile(reader,pSuperApi);
		else if (reader.name() == "Header")
			ReadHeaderFile(reader,pSuperApi);
		else
			reader.skipCurrentElement();
	}

	if(m_mapApi.find(pSuperApi->type)!=m_mapApi.end()){
		delete pSuperApi;
	}
	else{
		CSuperApi* pApi=CreateApi(pSuperApi);
		m_mapApi.insert(pSuperApi->type,pApi);
	}
}

void CSuperConfig::ReadType( QXmlStreamReader& reader,stSuperApi* pSuperApi )
{
	Q_ASSERT(reader.isStartElement() && reader.name() == "Type");
	pSuperApi->type=(ApiType)reader.readElementText().toInt();
}

void CSuperConfig::ReadAccess( QXmlStreamReader& reader,stSuperApi* pSuperApi )
{
	Q_ASSERT(reader.isStartElement() && reader.name() == "AuthAccess");
	pSuperApi->baAuthAccess=reader.readElementText().toAscii();
}

void CSuperConfig::ReadAccessScret( QXmlStreamReader& reader,stSuperApi* pSuperApi )
{
	Q_ASSERT(reader.isStartElement() && reader.name() == "AuthAccessScret");
	pSuperApi->baAuthAccessSecret=reader.readElementText().toAscii();
}

void CSuperConfig::ReadUserId( QXmlStreamReader& reader,stSuperApi* pSuperApi )
{
	Q_ASSERT(reader.isStartElement() && reader.name() == "UserId");
	pSuperApi->baUserId=reader.readElementText().toAscii();
}

void CSuperConfig::ReadIconFile( QXmlStreamReader& reader,stSuperApi* pSuperApi )
{
	Q_ASSERT(reader.isStartElement() && reader.name() == "Icon");
	pSuperApi->qsIconFile=reader.readElementText();
}

void CSuperConfig::ReadHeaderFile( QXmlStreamReader& reader,stSuperApi* pSuperApi )
{
	Q_ASSERT(reader.isStartElement() && reader.name() == "Header");
	pSuperApi->qsHeaderFile=reader.readElementText();
}

void CSuperConfig::WriteApi( QXmlStreamWriter& writer,stSuperApi* pSuperApi )
{
	writer.writeStartElement("API");
	writer.writeTextElement("Type",QString("%1").arg(pSuperApi->type));
	writer.writeTextElement("AuthAccess",pSuperApi->baAuthAccess);
	writer.writeTextElement("AuthAccessScret",pSuperApi->baAuthAccessSecret);
	writer.writeTextElement("UserId",pSuperApi->baUserId);
	writer.writeTextElement("Icon",pSuperApi->qsIconFile);
	writer.writeTextElement("Header",pSuperApi->qsHeaderFile);
	writer.writeEndElement();
}
